Check if Two Strings are Anagram (gfg)
Problem Description
Given two strings a
and b
consisting of lowercase characters, the task is to check whether the two given strings are an anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, act
and tac
are an anagram of each other. Strings a
and b
can only contain lowercase alphabets.
Examples
Example 1:
Input: a = "listen", b = "silent"
Output: YES
Explanation: The characters in both strings are the same.
Example 2:
Input: a = "hello", b = "billion"
Output: NO
Explanation: The characters in the strings are different.
Your Task
You don't need to read input or print anything. Your task is to complete the function isAnagram()
which takes the strings a
and b
as inputs and returns a boolean indicating whether the two strings are an anagram of each other.
Expected Time Complexity: for sorting-based approach or for counting-based approach.
Expected Auxiliary Space: for constant extra space.
Constraints
1 ≤ |a|, |b| ≤ 10^5
Problem Explanation
The problem is to check if two strings are anagram of each other, meaning both strings should have the same characters in any order.
Code Implementation
- Python
- C++
class Solution:
def isAnagram(self, a: str, b: str) -> bool:
# If lengths are different, they cannot be anagrams
if len(a) != len(b):
return False
# Sort and compare
return sorted(a) == sorted(b)
# Example usage
if __name__ == "__main__":
solution = Solution()
print(solution.isAnagram("listen", "silent")) # Expected output: True
print(solution.isAnagram("hello", "billion")) # Expected output: False
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
// Function to check whether two strings are anagram of each other or not.
bool isAnagram(string a, string b) {
// If lengths are different, they cannot be anagrams
if (a.size() != b.size()) return false;
// Sort both strings
sort(a.begin(), a.end());
sort(b.begin(), b.end());
// Compare sorted strings
return a == b;
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
while(t--) {
string c, d;
cin >> c >> d;
Solution obj;
if (obj.isAnagram(c, d)) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
// } Driver Code Ends
Example Walkthrough
For the strings a = "listen"
and b = "silent"
:
- Check the lengths of
a
andb
. They are both 6. - Sort both strings:
a
becomeseilnst
andb
becomeseilnst
. - Compare the sorted strings. They are equal, so the result is
YES
.
For the strings a = "hello"
and b = "billion"
:
- Check the lengths of
a
andb
.a
is 5 andb
is 7. - Since the lengths are different, the result is
NO
.
Solution Logic:
- Check if the lengths of the two strings are the same. If not, they cannot be anagrams.
- Sort both strings and compare them. If they are equal, the strings are anagrams.
Time Complexity
- The sorting-based approach has a time complexity of , where n is the length of the strings.
Space Complexity
- The auxiliary space complexity is for constant extra space.
References
- gfg Problem: gfg Problem
- Solution Author: arunimad6yuq